//
//  NetworkRequest.m
//  WuXi_DevelopStatisticMonitor
//
//  Created by Vergil on 2017/4/24.
//  Copyright © 2017年 Vergil. All rights reserved.
//

#import "NetworkRequest.h"

@interface NetworkRequest ()

@property (nonatomic, strong) AFHTTPSessionManager *httpManager;

@end

static NetworkRequest *networkRequest;

@implementation NetworkRequest

+ (instancetype)shareManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        networkRequest = [[NetworkRequest alloc] init];
    });
    return networkRequest;
    
}

- (instancetype)init {
    if (self = [super init]) {
        
        _httpManager = [AFHTTPSessionManager manager];
        _httpManager.requestSerializer.timeoutInterval = 15.f;
        [_httpManager.requestSerializer setValue:@"" forHTTPHeaderField:@""];
        
    }
    return self;
}

//单写登录方法
+ (void)loginRequestParameter:(NSDictionary *)parameter Success:(void(^)(NSDictionary *response))response Error:(void(^)(NSError *error))error {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager POST:@"" parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

//判断网络状态
- (void)CheckNetworkStatus {
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            //有网
        } else {
            [self showTextMessage:@"请检查你的网络连接"];
        }
        
    }];
    
}

//显示信息 1.5秒后隐藏
- (void)showTextMessage:(NSString *)message {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:1.5f];
}

- (void)dismissAlert:(UIAlertController *)alert {
    if (alert) {
        [alert dismissViewControllerAnimated:YES completion:nil];
        alert = nil;
    }
}

@end
